import Vuex from 'vuex'
import counter from './counter'

export default new Vuex.Store({
  modules: {
    counter
  },
  state: {
    systemInfo: null,
    navbarHeight: null,
    userInfo: {},
    userStatus: {},
    likeList: [],
    posterInfo: {},
    config: {},
    adList: [],
    currVideoInfo: {},
    sharerUserId: 0,
    elan: {}
  },
  mutations: {
    ['GET_SYSTEMINFO_SUCCESS'](state, systemInfo) {
      state.systemInfo = systemInfo;
    },
    ['SET_NavBar_height'](state, data) {
      state.navbarHeight = data;
    },
    ['SET_USERINFO'](state, data) {
      state.userInfo = data
    },
    ['SET_USER'](state, data) {
      state.userStatus = data;
    },
    ['SET_LIKE'](state, data) {
      state.likeList = data;
    },
    ['SET_POSTER'](state, data) {
      state.posterInfo = data;
    },
    ['SET_CONF'](state, data) {
      state.config = data;
    },
    ['SET_AD'](state, data) {
      state.adList = data;
    },
    ['SET_VINFO'](state, data) {
      state.currVideoInfo = data;
    },
    ['SET_SHARE_UID'](state, data) {
      state.sharerUserId = data;
    },
    ['SET_ELAN'](state, data) {
      state.elan = data;
    }
  },
  actions: {
    getSystemInfo({
      commit,
      state
    }) {
      return new Promise((resolve, reject) => {
        if (state.systemInfo) {
          resolve(state.systemInfo)
        } else {
          wx.getSystemInfo({
            success(res) {
              let custom = wx.getMenuButtonBoundingClientRect();
              let customBar = custom.bottom + custom.top - res.statusBarHeight;
              commit('SET_NavBar_height', customBar);
              commit('GET_SYSTEMINFO_SUCCESS', res)
              resolve(res)
            },
            fail(err) {
              reject(err)
            }
          });
        }

      })
    },
  },
  getters: {
    statusBarHeight: state => {
      return state.systemInfo.statusBarHeight
    },
    customBar: state => {
      let custom = wx.getMenuButtonBoundingClientRect();
      let customBar = custom.bottom + custom.top - state.systemInfo.statusBarHeight;
      return customBar;
    },
    statusBar: state => state.systemInfo.statusBarHeight,
    clientRect: state => wx.getMenuButtonBoundingClientRect()
  }
})